package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.PcFeedbackHandleDto;
import com.dog.basics.bean.dto.admin.common.PcFeedbackPageDto;
import com.dog.basics.bean.dto.api.common.FeedbackSaveDto;
import com.dog.basics.bean.model.common.DogFeedback;
import com.dog.basics.bean.vo.admin.common.PcFeedbackPageVo;
import com.dog.basics.common.util.Json;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.IDogFeedbackMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.IDogFeedbackService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 意见反馈 服务实现类
 * </p>
 *
 * @author fengjukang
 * @since 2019-10-11
 */
@Service
@AllArgsConstructor
public class DogFeedbackServiceImpl extends ServiceImpl<IDogFeedbackMapper, DogFeedback> implements IDogFeedbackService {

    /**
     * 新增意见反馈
     *
     * @param dto
     * @return
     * @author guonh
     */
    @Override
    public Boolean addFeedback(FeedbackSaveDto dto) {
        // 判断手机号码
        Validator.validateMobile(dto.getTelephone(), "手机号码格式错误");
        // 执行添加数据
        DogFeedback addFeedback = BeanUtil.toBean(dto, DogFeedback.class);
        addFeedback.setCreateBy(SecurityUtils.getUser().getUserId());
        addFeedback.setCreateTime(DateUtil.currentSeconds());
        addFeedback.setFeedbackImgArray(JSONUtil.toJsonStr(dto.getFeedbackImgList()));
        Validator.validateTrue(this.save(addFeedback), "意见反馈提交失败,请稍后重试");
        return Boolean.TRUE;
    }

    /**
     * 意见反馈列表
     *
     * @param page
     * @param dto
     * @return
     * @author guonh
     */
    @Override
    public IPage<PcFeedbackPageVo> getFeedbackPage(PageParam page, PcFeedbackPageDto dto) {
        // 查询数据信息
        this.page(page, Wrappers.<DogFeedback>lambdaQuery()
                .eq(ObjectUtil.isNotNull(dto.getFeedbackType()), DogFeedback::getFeedbackType, dto.getFeedbackType())
                .like(StrUtil.isNotBlank(dto.getTelephone()), DogFeedback::getTelephone, dto.getTelephone())
                .orderBy(Boolean.TRUE, dto.getIsOrderByAvg(), DogFeedback::getCreateTime));
        // 数据信息转型格式化
        List<DogFeedback> records = page.getRecords();
        page.setRecords(records.stream().map(e -> {
            PcFeedbackPageVo toBean = BeanUtil.toBean(e, PcFeedbackPageVo.class);
            List<String> ts = null;
            try {
                //ts = JSONUtil.toBean(e.getFeedbackImgArray(), ArrayList.class);
                ts= Json.parseArray(e.getFeedbackImgArray(),String[].class);
            } catch (Exception ex) {
                ts = CollUtil.newArrayList();
            }
            toBean.setFeedbackImgArray(ts);
            return toBean;
        }).collect(Collectors.toList()));
        return page;
    }


    /**
     * 删除意见反馈
     *
     * @param ids
     * @return
     * @author guonh
     */
    @Override
    public Boolean delFeedback(List<Long> ids) {
        // 判断是否为空
        Validator.validateTrue(CollUtil.isNotEmpty(ids), "意见标识为空");
        // 删除意见信息
        Validator.validateTrue(this.removeByIds(ids), "删除失败");
        return Boolean.TRUE;
    }

    /**
     * 意见处理
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean editFeedback(PcFeedbackHandleDto dto) {
        // 判断意见是否存在
        DogFeedback feedback = this.getById(dto.getId());
        Validator.validateNotNull(feedback, "反馈意见不存在");
        // 判断如果处理已读状态则判断当前意见反馈是否未读状态
        if (Validator.equal(dto.getFeedbackState(), 1)) {
            Validator.validateEqual(feedback.getFeedbackState(), 0, "反馈意见状态,不是未读状态不可以修改为已读!");
        }
        // 数据处理修改
        DogFeedback feedback1 = BeanUtil.toBean(dto, DogFeedback.class);
        Validator.validateTrue(this.updateById(feedback1), "修改意见出错");
        return Boolean.TRUE;
    }
}
